//
//  SESettingViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/15.
//

import UIKit

class SESettingViewController: SEBaseViewController {

    @IBOutlet weak var appVersionLabel: UILabel! {
        didSet {
            appVersionLabel.text = "V\(UIDevice.appVersion)"
        }
    }

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = tableFooterView
            tableView.rowHeight = 50
            tableView.separatorColor = UIColor.init(hex: "EBECF0")
            tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            tableView.register(UINib(nibName: SESettingListCell.className, bundle: nil), forCellReuseIdentifier: SESettingListCell.reuseId)
        }
    }

    var viewModel = SESettingViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "系统设定"
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()
        viewModel.updateData = { status in
            switch status {
            case .success:
                self.logoutSucess()
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private lazy var tableFooterView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 30 + 48 + 30))
        let btnView = SEConfirmBtnView(frame: CGRect(x: 0, y: 0, width: 150, height: 48))
        btnView.center = view.center
        btnView.delegate = self
        btnView.title = "退出登录"
        view.addSubview(btnView)
        return view
    }()

    private func pushToAppStore() {
        UIApplication.shared.open(URL(string: "https://apps.apple.com/cn/app/id1569548072")!, options: [:], completionHandler: nil)
    }

}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SESettingViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SESettingListCell.reuseId, for: indexPath) as! SESettingListCell
        cell.updateCell(indexPath: indexPath)
        return cell
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let cell = tableView.cellForRow(at: indexPath) as? SESettingListCell else {
            return
        }
        switch cell.type {
        case .update:
            pushToAppStore() 
        }
    }

}

// MARK: - SEConfirmBtnViewDelegate

extension SESettingViewController: SEConfirmBtnViewDelegate {
    func confirmBtnViewAction(view: SEConfirmBtnView) {
        logout()
    }

    func logoutSucess() {
        UserTools.removeAllUserInfo()
        presentLoginViewController(selector: nil)
    }
}

// APIRequest

extension SESettingViewController {
    private func logout() {
        viewModel.logout()
    }
}
